package com.shop2cn.iapi.grouponmgr.testcase;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.grouponmgr.parameter.CheckConflictProductsBean;
import com.shop2cn.iapi.grouponmgr.parameter.SaveGrouponReq;
import com.shop2cn.iapi.grouponmgr.service.CheckConflictProductsCall;
import com.shop2cn.iapi.agentproducts.parameter.AddAgentProductBean;
import com.shop2cn.iapi.agentproducts.parameter.ImportAgentProductBean;
import com.shop2cn.iapi.agentproducts.parameter.ModifyAgentProductBean;
import com.shop2cn.iapi.agentproducts.parameter.ProductDetailsDto;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.AddAgentProductCallService;
import com.ymttest.business.service.Shop2cnGrouponCallService;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.utils.AgentUsers;
import com.ymttest.utils.YMTDateUtil;

/**
 * 检查冲突商品Test FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_CheckConflictProducts {
	private static CheckConflictProductsBean checkconflictproductsBean;
	private static CheckConflictProductsCall checkconflictproductsCall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("检查冲突商品Test");
	}

	@Before
	public void caseUp() {
		checkconflictproductsBean = new CheckConflictProductsBean();
		checkconflictproductsCall = new CheckConflictProductsCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(checkconflictproductsCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_CheckConflictProducts_001() {
		Logger.start(true, "检测到时间段内已经拼团的商品");
		try {
			Integer mchId = GodmchManageCallService.CreateSellerMch();
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(mchId, "AutoTest",
					false);
			String pid = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			List<String> catalogList = new ArrayList<String>();
			ProductDetailsDto selectProductDetails = AddAgentProductCallService.selectProductDetails(mchId, pid);
			selectProductDetails.getCatalogList().forEach(a -> {
				catalogList.add(a.getCatalogId());
			});

			SaveGrouponReq savegrouponBean = Shop2cnGrouponCallService.initSaveGrouponReq(mchId, pid,
					catalogList.get(0), 100d);
			savegrouponBean.setBeginTime(YMTDateUtil.getBeforeOrNextDay(-2, "yyyy-MM-dd HH:mm:ss"));
			savegrouponBean.setEndTime(YMTDateUtil.getBeforeOrNextDay(3, "yyyy-MM-dd HH:mm:ss"));
			Integer grouponProductId = Shop2cnGrouponCallService.SaveGrouponCall(savegrouponBean);

			checkconflictproductsBean.setSellerId(mchId);
			checkconflictproductsBean.setProductIdList(Arrays.asList(pid));
			checkconflictproductsBean.setBegin(YMTDateUtil.getBeforeOrNextDay(-1, "yyyy-MM-dd HH:mm:ss"));
			checkconflictproductsBean.setEnd(YMTDateUtil.getBeforeOrNextDay(2, "yyyy-MM-dd HH:mm:ss"));
				
			
			checkconflictproductsCall.setData(checkconflictproductsBean);
			checkconflictproductsCall.callService();
			Logger.verifyEquals("1001", checkconflictproductsCall.getString("code"), "验证code");
			String productid = checkconflictproductsCall.getJsonObject().getAsJsonArray("data").get(0).getAsJsonObject().get("productId").getAsString();
			Logger.verifyEquals(pid, productid,"验证商品id");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_CheckConflictProducts_002() {
		Logger.start(true, "拼团时间  2 5  查询 1 3");
		try {
			Integer mchId = GodmchManageCallService.CreateSellerMch();
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(mchId, "AutoTest",
					false);
			String pid = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			List<String> catalogList = new ArrayList<String>();
			ProductDetailsDto selectProductDetails = AddAgentProductCallService.selectProductDetails(mchId, pid);
			selectProductDetails.getCatalogList().forEach(a -> {
				catalogList.add(a.getCatalogId());
			});

			SaveGrouponReq savegrouponBean = Shop2cnGrouponCallService.initSaveGrouponReq(mchId, pid,
					catalogList.get(0), 100d);
			savegrouponBean.setBeginTime(YMTDateUtil.getBeforeOrNextDay(2, "yyyy-MM-dd HH:mm:ss"));
			savegrouponBean.setEndTime(YMTDateUtil.getBeforeOrNextDay(5, "yyyy-MM-dd HH:mm:ss"));
			Integer grouponProductId = Shop2cnGrouponCallService.SaveGrouponCall(savegrouponBean);

			checkconflictproductsBean.setSellerId(mchId);
			checkconflictproductsBean.setProductIdList(Arrays.asList(pid));
			checkconflictproductsBean.setBegin(YMTDateUtil.getBeforeOrNextDay(1, "yyyy-MM-dd HH:mm:ss"));
			checkconflictproductsBean.setEnd(YMTDateUtil.getBeforeOrNextDay(3, "yyyy-MM-dd HH:mm:ss"));
				
			
			checkconflictproductsCall.setData(checkconflictproductsBean);
			checkconflictproductsCall.callService();
			Logger.verifyEquals("1001", checkconflictproductsCall.getString("code"), "验证code");
			String productid = checkconflictproductsCall.getJsonObject().getAsJsonArray("data").get(0).getAsJsonObject().get("productId").getAsString();
			Logger.verifyEquals(pid, productid,"验证商品id");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_CheckConflictProducts_003() {
		Logger.start(true, "拼团时间  2 5 查询  3 6");
		try {
			Integer mchId = GodmchManageCallService.CreateSellerMch();
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(mchId, "AutoTest",
					false);
			String pid = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			List<String> catalogList = new ArrayList<String>();
			ProductDetailsDto selectProductDetails = AddAgentProductCallService.selectProductDetails(mchId, pid);
			selectProductDetails.getCatalogList().forEach(a -> {
				catalogList.add(a.getCatalogId());
			});

			SaveGrouponReq savegrouponBean = Shop2cnGrouponCallService.initSaveGrouponReq(mchId, pid,
					catalogList.get(0), 100d);
			savegrouponBean.setBeginTime(YMTDateUtil.getBeforeOrNextDay(2, "yyyy-MM-dd HH:mm:ss"));
			savegrouponBean.setEndTime(YMTDateUtil.getBeforeOrNextDay(5, "yyyy-MM-dd HH:mm:ss"));
			Integer grouponProductId = Shop2cnGrouponCallService.SaveGrouponCall(savegrouponBean);

			checkconflictproductsBean.setSellerId(mchId);
			checkconflictproductsBean.setProductIdList(Arrays.asList(pid));
			checkconflictproductsBean.setBegin(YMTDateUtil.getBeforeOrNextDay(3, "yyyy-MM-dd HH:mm:ss"));
			checkconflictproductsBean.setEnd(YMTDateUtil.getBeforeOrNextDay(6, "yyyy-MM-dd HH:mm:ss"));
				
			
			checkconflictproductsCall.setData(checkconflictproductsBean);
			checkconflictproductsCall.callService();
			Logger.verifyEquals("1001", checkconflictproductsCall.getString("code"), "验证code");
			String productid = checkconflictproductsCall.getJsonObject().getAsJsonArray("data").get(0).getAsJsonObject().get("productId").getAsString();
			Logger.verifyEquals(pid, productid,"验证商品id");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_CheckConflictProducts_004() {
		Logger.start(true, "拼团时间  2 5 查询  3 4");
		try {
			Integer mchId = GodmchManageCallService.CreateSellerMch();
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(mchId, "AutoTest",
					false);
			String pid = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			List<String> catalogList = new ArrayList<String>();
			ProductDetailsDto selectProductDetails = AddAgentProductCallService.selectProductDetails(mchId, pid);
			selectProductDetails.getCatalogList().forEach(a -> {
				catalogList.add(a.getCatalogId());
			});

			SaveGrouponReq savegrouponBean = Shop2cnGrouponCallService.initSaveGrouponReq(mchId, pid,
					catalogList.get(0), 100d);
			savegrouponBean.setBeginTime(YMTDateUtil.getBeforeOrNextDay(1, "yyyy-MM-dd HH:mm:ss"));
			savegrouponBean.setEndTime(YMTDateUtil.getBeforeOrNextDay(3, "yyyy-MM-dd HH:mm:ss"));
			Integer grouponProductId = Shop2cnGrouponCallService.SaveGrouponCall(savegrouponBean);

			checkconflictproductsBean.setSellerId(mchId);
			checkconflictproductsBean.setProductIdList(Arrays.asList(pid));
			checkconflictproductsBean.setBegin(YMTDateUtil.getBeforeOrNextDay(-1, "yyyy-MM-dd HH:mm:ss"));
			checkconflictproductsBean.setEnd(YMTDateUtil.getBeforeOrNextDay(3, "yyyy-MM-dd HH:mm:ss"));
				
			
			checkconflictproductsCall.setData(checkconflictproductsBean);
			checkconflictproductsCall.callService();
			Logger.verifyEquals("1001", checkconflictproductsCall.getString("code"), "验证code");
			String productid = checkconflictproductsCall.getJsonObject().getAsJsonArray("data").get(0).getAsJsonObject().get("productId").getAsString();
			Logger.verifyEquals(pid, productid,"验证商品id");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_CheckConflictProducts_005() {
		Logger.start(true, "拼团时间  2 5  查询  1 6");
		try {
			Integer mchId = GodmchManageCallService.CreateSellerMch();
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(mchId, "AutoTest",
					false);
			String pid = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			List<String> catalogList = new ArrayList<String>();
			ProductDetailsDto selectProductDetails = AddAgentProductCallService.selectProductDetails(mchId, pid);
			selectProductDetails.getCatalogList().forEach(a -> {
				catalogList.add(a.getCatalogId());
			});

			SaveGrouponReq savegrouponBean = Shop2cnGrouponCallService.initSaveGrouponReq(mchId, pid,
					catalogList.get(0), 100d);
			savegrouponBean.setBeginTime(YMTDateUtil.getBeforeOrNextDay(1, "yyyy-MM-dd HH:mm:ss"));
			savegrouponBean.setEndTime(YMTDateUtil.getBeforeOrNextDay(3, "yyyy-MM-dd HH:mm:ss"));
			Integer grouponProductId = Shop2cnGrouponCallService.SaveGrouponCall(savegrouponBean);

			checkconflictproductsBean.setSellerId(mchId);
			checkconflictproductsBean.setProductIdList(Arrays.asList(pid));
			checkconflictproductsBean.setBegin(YMTDateUtil.getBeforeOrNextDay(-1, "yyyy-MM-dd HH:mm:ss"));
			checkconflictproductsBean.setEnd(YMTDateUtil.getBeforeOrNextDay(3, "yyyy-MM-dd HH:mm:ss"));
				
			
			checkconflictproductsCall.setData(checkconflictproductsBean);
			checkconflictproductsCall.callService();
			Logger.verifyEquals("1001", checkconflictproductsCall.getString("code"), "验证code");
			String productid = checkconflictproductsCall.getJsonObject().getAsJsonArray("data").get(0).getAsJsonObject().get("productId").getAsString();
			Logger.verifyEquals(pid, productid,"验证商品id");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_CheckConflictProducts_006() {
		Logger.start(true, "商品被下架，查询冲突--有冲突");
		try {
			Integer mchId = GodmchManageCallService.CreateSellerMch();
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(mchId, "AutoTest",
					false);
			String pid = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			List<String> catalogList = new ArrayList<String>();
			ProductDetailsDto selectProductDetails = AddAgentProductCallService.selectProductDetails(mchId, pid);
			selectProductDetails.getCatalogList().forEach(a -> {
				catalogList.add(a.getCatalogId());
			});

			SaveGrouponReq savegrouponBean = Shop2cnGrouponCallService.initSaveGrouponReq(mchId, pid,
					catalogList.get(0), 100d);
			savegrouponBean.setBeginTime(YMTDateUtil.getBeforeOrNextDay(1, "yyyy-MM-dd HH:mm:ss"));
			savegrouponBean.setEndTime(YMTDateUtil.getBeforeOrNextDay(3, "yyyy-MM-dd HH:mm:ss"));
			Integer grouponProductId = Shop2cnGrouponCallService.SaveGrouponCall(savegrouponBean);

			Thread.sleep(2000);
			AddAgentProductCallService.SetOffshelf(pid, mchId);

			Thread.sleep(2000);
			checkconflictproductsBean.setSellerId(mchId);
			checkconflictproductsBean.setProductIdList(Arrays.asList(pid));
			checkconflictproductsBean.setBegin(YMTDateUtil.getBeforeOrNextDay(-1, "yyyy-MM-dd HH:mm:ss"));
			checkconflictproductsBean.setEnd(YMTDateUtil.getBeforeOrNextDay(3, "yyyy-MM-dd HH:mm:ss"));
				
			
			checkconflictproductsCall.setData(checkconflictproductsBean);
			checkconflictproductsCall.callService();
			Logger.verifyEquals("1001", checkconflictproductsCall.getString("code"), "验证code");
			String productid = checkconflictproductsCall.getJsonObject().getAsJsonArray("data").get(0).getAsJsonObject().get("productId").getAsString();
			Logger.verifyEquals(pid, productid,"验证商品id");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	//vip渠道
		private static int vipRootUserId = AgentUsers.vipRootUserId;
		private static String vipRootUserName = "NickautoTest";
		private static int csId = AgentUsers.csId;
		private static String csName = "NickautoTest";
		//vip渠道->代理
		private static int vipAgentUserId1 = AgentUsers.vipAgentUserId1;
		private static String vipAgentUserName1 = "dgsq408897016";
		private static int csId1 = AgentUsers.csId1;
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_CheckConflictProducts_007() {
		Logger.start(true, "商品代理价变更");
		try {
			//vip渠道
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(vipRootUserId,vipRootUserName,true);
			
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			//vip代理1
			ImportAgentProductBean importAgentProductBean = AddAgentProductCallService.initImportAgentProduct(vipAgentUserId1,vipAgentUserName1,productId,vipRootUserId,true);
			String pid = AddAgentProductCallService.ImportAgentProduct(importAgentProductBean);
			
			
			List<String> catalogList =new ArrayList<String>();
			ProductDetailsDto selectProductDetails = AddAgentProductCallService.selectProductDetails(vipAgentUserId1,pid);
			selectProductDetails.getCatalogList().forEach(a->{
		 		catalogList.add(a.getCatalogId());
		 	});
			
			SaveGrouponReq savegrouponBean = Shop2cnGrouponCallService.initSaveGrouponReq(vipAgentUserId1, pid, catalogList, 200d);
			Integer grouponProductId = Shop2cnGrouponCallService.SaveGrouponCall(savegrouponBean);
			
			
			
			ModifyAgentProductBean initModifyAgentProductBean = AddAgentProductCallService.initModifyAgentProductBean(productId, addAgentProductBean);
			initModifyAgentProductBean.getCatalogList().get(0).getAgentPriceList().get(0).setAgentPrice(201d);
			AddAgentProductCallService.modifyAgentProduct(initModifyAgentProductBean);
			
//			ModifyAgentProductBean initModifyAgentProductBeanByImportant = AddAgentProductCallService.initModifyAgentProductBeanByImportant(pid, importAgentProductBean);
//			double  price1 = 10000d;
//			List<DetailsCatalogDto> catalog = selectProductDetails.getCatalogList();
//			for (int i = 0; i < catalog.size(); i++) {
//				double agentPrice = catalog.get(i).getAgentPrice();
//				price1=agentPrice<price1?agentPrice:price1;
//			}
////			initModifyAgentProductBeanByImportant.getCatalogList().get(0).setDirectPrice(price1);
//			initModifyAgentProductBeanByImportant.getCatalogList().get(0).setMarketPrice(price1);
//			AddAgentProductCallService.modifyAgentProduct(initModifyAgentProductBeanByImportant);
			
			Thread.sleep(2000);
			checkconflictproductsBean.setSellerId(vipAgentUserId1);
			checkconflictproductsBean.setProductIdList(Arrays.asList(pid));
			checkconflictproductsBean.setBegin(YMTDateUtil.getBeforeOrNextDay(-1, "yyyy-MM-dd HH:mm:ss"));
			checkconflictproductsBean.setEnd(YMTDateUtil.getBeforeOrNextDay(3, "yyyy-MM-dd HH:mm:ss"));
				
			
			checkconflictproductsCall.setData(checkconflictproductsBean);
			checkconflictproductsCall.callService();
			Logger.verifyEquals("1001", checkconflictproductsCall.getString("code"), "验证code");
			String productid = checkconflictproductsCall.getJsonObject().getAsJsonArray("data").get(0).getAsJsonObject().get("productId").getAsString();
			Logger.verifyEquals(pid, productid,"验证商品id");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}